package com.carbon.trade.controller;

import com.carbon.trade.entity.EsCarbonMethodology;
import com.carbon.trade.repository.MethodologyRepository;
import com.carbon.domain.common.ApiResult;
import com.carbon.common.api.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class TestController
{
    @Autowired
    private MethodologyRepository methodologyRepository;

    @PostMapping("/save")
    public void testSave(@RequestBody EsCarbonMethodology esCarbonMethodology)
    {
        System.out.println(esCarbonMethodology);
        methodologyRepository.save(esCarbonMethodology);
    }

    @GetMapping("/getById")
    public void findById(String id)
    {
        Page<EsCarbonMethodology> methodologies= methodologyRepository.findById(id, PageRequest.of(0,10));
        for (EsCarbonMethodology esCarbonMethodology :methodologies.getContent())
        {
            System.out.println(esCarbonMethodology);
        }
    }

    @GetMapping("/getByName")
    public void findByName(String name)
    {
        Page<EsCarbonMethodology> methodologies= methodologyRepository.findByName(name, PageRequest.of(0,10));
        for (EsCarbonMethodology esCarbonMethodology :methodologies.getContent())
        {
            System.out.println(esCarbonMethodology);
        }
    }

    @GetMapping("/getByContent")
    public void findByKeyword(String keyword)
    {
        Page<EsCarbonMethodology> methodologies= methodologyRepository.findByContentOrName(keyword,keyword, PageRequest.of(0,10));
        for (EsCarbonMethodology esCarbonMethodology :methodologies.getContent())
        {
            System.out.println(esCarbonMethodology);
        }
    }

    @PostMapping("/update")
    public void update(EsCarbonMethodology esCarbonMethodology)
    {
        Page<EsCarbonMethodology> methodologies= methodologyRepository.findById(esCarbonMethodology.getId(), PageRequest.of(0,10));
        EsCarbonMethodology newcarbonMethodologyEs =methodologies.getContent().get(0);
        methodologyRepository.save(newcarbonMethodologyEs);
    }

    @PostMapping("/delete")
    public void delete(String id)
    {
        Page<EsCarbonMethodology> methodologies= methodologyRepository.findById(id, PageRequest.of(0,10));
        EsCarbonMethodology esCarbonMethodology =methodologies.getContent().get(0);
        methodologyRepository.delete(esCarbonMethodology);
    }

    /**
     * 测试碳交易供需行情API
     */
    @PostMapping("/trade/carbonTradeQuote/getPageList")
    public ApiResult<Paging<Object>> getCarbonTradeQuoteList(@RequestBody Object queryParam) {
        try {
            log.info("测试碳交易供需行情API，参数: {}", queryParam);
            
            // 返回模拟数据
            List<Object> mockData = new ArrayList<>();
            Paging<Object> paging = new Paging<>(0, 1, mockData);
            
            return ApiResult.ok(paging);
        } catch (Exception e) {
            log.error("测试碳交易供需行情API失败", e);
            return ApiResult.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 测试数据库连接
     */
    @GetMapping("/test-db")
    public ApiResult<String> testDatabase() {
        try {
            // 简单的数据库查询测试
            long count = methodologyRepository.count();
            return ApiResult.ok("数据库连接正常，方法学数据条数: " + count);
        } catch (Exception e) {
            log.error("数据库连接测试失败", e);
            return ApiResult.fail("数据库连接失败: " + e.getMessage());
        }
    }
}
